package com.itsxb.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.itsxb.constants.Constants;
import com.itsxb.constants.GlobalExceptionCode;
import com.itsxb.pojo.domain.DriverSetting;
import com.itsxb.mapper.DriverSettingMapper;
import com.itsxb.pojo.vo.DriverLocation2GeoVo;
import com.itsxb.service.IDriverSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itsxb.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 司机配置 服务实现类
 * </p>
 *
 * @author 奇天书
 * @since 2023-12-22
 */
@Service
public class DriverSettingServiceImpl extends ServiceImpl<DriverSettingMapper, DriverSetting> implements IDriverSettingService {
    @Autowired
    private DriverSettingMapper driverSettingMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void initDriverSetting(Long id) {
        DriverSetting driverSetting = new DriverSetting();
        driverSetting.setId(id);
        driverSetting.setCreateTime(new Date());
        super.save(driverSetting);
    }

    @Override
    public void online() {
        //1.不需要JSR303 校验
        //2.业务校验
        long loginId = StpUtil.getLoginIdAsLong();
        //2.1 去查询司机设置表中是否有根据loginId查询出来的数据
        DriverSetting driverSetting = driverSettingMapper.selectById(loginId);
        AssertUtil.isNotNull(driverSetting, GlobalExceptionCode.SERVICE_ERROR);
        //3.业务实现
        //3.1 将格式化放在Redis中的司机上线的Key
        String driverOnlineKey = String.format(Constants.Redis.DRIVER_REIDS_SETTING_KEY, loginId);
        //将司机的设置表放在redis中 需要设置过期时间 过期之后就会从redis删除这条数据
        redisTemplate.opsForValue().set(driverOnlineKey, driverSetting, 5000, TimeUnit.MINUTES);

    }

    @Override
    public void offline() {
        long loginId = StpUtil.getLoginIdAsLong();
        String driverOnlineKey = String.format(Constants.Redis.DRIVER_REIDS_SETTING_KEY, loginId);
        //司机下线 删除redis中的 司机设置表
        redisTemplate.delete(driverOnlineKey);
        //不仅要删除redid中的司机配置表 还要删除司机的Geo坐标
        redisTemplate.opsForGeo().remove(Constants.Redis.DRIVER_REIDS_GEO_KEY,loginId);

    }

    @Override
    public void driverLocation2Geo(DriverLocation2GeoVo driverLocation2GeoVo) {
        //1.JSR303参数检验
        //2.业务检验
        //获取loginId
        long loginId = StpUtil.getLoginIdAsLong();
        //2.1 需要校验Redis中的存储的设置信息 是否存在  也就是说判断是否上线 防止那种一直不动的人
        String RedisSettingKey = String.format(Constants.Redis.DRIVER_REIDS_SETTING_KEY, loginId);
        //前端和后端规则不一样 司机需要知道自己未移动5分钟 之后停止接单的提示
        AssertUtil.isNotNull(RedisSettingKey,GlobalExceptionCode.DRIVER_OFFLINE);
        //3.业务实现
        //添加到Redis的Geo数据结构
        redisTemplate.opsForGeo().add(
                Constants.Redis.DRIVER_REIDS_GEO_KEY
                , new Point(driverLocation2GeoVo.getLongitude()
                , driverLocation2GeoVo.getLatitude())
                , loginId
        );
        //这是续约 如果Geo改变了 就会给配置信续约5分钟
        redisTemplate.expire(RedisSettingKey,5,TimeUnit.MINUTES);


    }
}
